<template>
  <div>
    <el-dialog title="修改支行信息" :visible.sync="showEditBranchDialog" center width="700px">
      <el-form :model="branchData"  :rules="rules" ref="branchData" >

        <el-form-item label="支行名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="branchData.name" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="branchData.status" :label="'0'">停用</el-radio>
          <el-radio v-model="branchData.status" :label="'1'">启用</el-radio>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="submitBranch('branchData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from '@/store'
  import {edit} from '@/api/thk/cms/bankBranch'

  export default {
    name: 'editBranch',

    data() {
      return {
        showEditBranchDialog: false,
        formLabelWidth: '130px',

        branchData: {
          bank_branch_id: '',
          name: '',
          status: '',
        },

        rules: {
          // 主行名字必须
          name:[
            { required: true, message: '请输入支行名称', trigger: 'blur' },
          ],
        }
      }
    },


    methods: {
      // 判断是否点击了提交
      submitBranch(branchData){
        this.$refs[branchData].validate((valid) => {
          if (valid) {

            console.log('====== edit branch =====')
            console.log(this.branchData)

            edit(store.getters.token,this.branchData).then(result => {
              // 增加成功
              this.showEditBranchDialog = !this.showEditBranchDialog
              this.$parent.getBranchData()
            }).catch(error => {
              console.log(error)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },

      // 显示dialog,并将数据回显
      showEditDialog(row){
        this.branchData.bank_branch_id = row.bank_branch_id
        this.branchData.name = row.name
        this.branchData.status = row.status+''

        this.showEditBranchDialog = !this.showEditBranchDialog
      },
      cancelDialog(){
        this.showEditBranchDialog = !this.showEditBranchDialog
      }

    }
  }
</script>
